library(Ryacas)
library(plotly)
source("HelperFunction.r")

## 1.符号计算 ====

# 创建符号变量
u <- ysym("u")
v <- ysym("v")

# 定义曲面
r = c(2*sin(u)*cos(v),2*sin(u)*sin(v),2*cos(u))
# 符号计算
ru1 = deriv(r,u)[1,]
ru2 = deriv(ru1,u)[1,]
rv1 = deriv(r,v)[1,]
rv2 = deriv(rv1,v)[1,]

ruv = deriv(ru1,v)[1,]

E = dot(ru1,ru1)
FF = dot(ru1,rv1)
G = dot(rv1,rv1)

n = norms(cross(ru1,rv1)) #需要归一化
L = dot(ru2,n)
M = dot(ruv,n)
N = dot(rv2,n)

y_eval(N, u = 1,v = 1,as.r = T)
# 高斯曲率的符号函数
Gauss_k = ( L * N - M*M)/(E*G - FF^2)

## 2.绘制曲面 ====

ux = seq(0.01,2*pi,len = 100)
uy = seq(0.01,pi,len = 100)

x = rep(1,length(ux)*length(uy))
y = x
z = x
for(i in 1:length(ux)){
  for(j in 1:length(uy)){
    
  }
}
x = outer(ux, uy, function(x, y) { y_eval(r[1],u = x,v = y,as.r = T) })
y = outer(ux, uy, function(x, y) { y_eval(r[2],u = x,v = y,as.r = T) })
z = outer(ux, uy, function(x, y) { y_eval(r[3],u = x,v = y,as.r = T) })
value = outer(ux, uy, function(x, y) { y_eval(Gauss_k,u = x,v = y,as.r = T) })

dim(x) = length(x)
dim(y) = length(y)
dim(z) = length(z)
dim(value) = length(value)
value = round(value,4)

plot_ly(x = x,y = y,z = z,color = value,type = "scatter3d",size = 0.4,mode = "lines")

